package com.FH.organization.remind;

import com.FH.applyForm.officeServies.noticemember.NoticeMemberPullDTO;
import com.FH.applyForm.officeServies.noticemember.NoticeMemberResource;
import com.FH.organization.notice.NoticeResource;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import javax.inject.Inject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/11/2.
 */
@Stateless
public class RemindDAOImpl extends AccessDB implements RemindDAO {


    @Inject
    RemindResource remindResource;
    @Inject
    NoticeMemberResource noticeMemberResource;

    @Inject
    NoticeResource noticeResource;
    /**
     * 查询提醒
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getRemindInfo(String search, String page, String size) {


        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from remind where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if (json.has("remind_order")) {
                sql_w += " AND remind_order ='" + json.getString("remind_order") + "'";
            } else {
                sql_w += " AND (remind_order IS NULL OR remind_order=0)";
            }


            if (json.has("remind_id")) {
                sql_w += " AND remind_id = '" + json.getString("remind_id") + "'";
            }

            if (json.has("remind_member_id")) {
                sql_w += " AND remind_member_id = '" + json.getString("remind_member_id") + "'";
            }
            if (json.has("remind_form_id")) {
                sql_w += " AND remind_form_id = '" + json.getString("remind_form_id") + "'";
            }
            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<RemindShowDTO> result = new ArrayList<RemindShowDTO>();
            if (!existName.equals("0")) {
                String sql = "select remind_id,remind_member_id,remind_content,remind_time,remind_type,remind_form_id,remind_order,remind_endtime,create_time,remind_starttime,remind_descritpion  from remind  where 1=1 " + sql_w + " ORDER BY create_time DESC " + sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    RemindShowDTO remindShowDTO = new RemindShowDTO();
                    remindShowDTO.setRemind_id(seresult.get(i)[1]);
                    remindShowDTO.setRemind_member_id(seresult.get(i)[2]);
                    remindShowDTO.setRemind_content(seresult.get(i)[3]);
                    remindShowDTO.setRemind_time(seresult.get(i)[4]);
                    remindShowDTO.setRemind_type(seresult.get(i)[5]);
                    remindShowDTO.setRemind_form_id(seresult.get(i)[6]);
                    remindShowDTO.setRemind_order(seresult.get(i)[7]);
                    remindShowDTO.setRemind_endtime(seresult.get(i)[8]);
                    remindShowDTO.setCreate_time(seresult.get(i)[9]);
                    remindShowDTO.setRemind_starttime(seresult.get(i)[10]);
                    remindShowDTO.setRemind_descritpion(seresult.get(i)[11]);


                    result.add(remindShowDTO);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStr(result.size() + "");
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

            resultDTO.setValues(result);


        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resultDTO;
    }

    /**
     * 添加提醒
     * @param remindPullDTO
     * @return
     */
    @Override
    public ResultDTO addRemindInfo(RemindPullDTO remindPullDTO) {
        ResultDTO resultDTO = new ResultDTO();
        int resultls = 0;

        String existName = "";
        /**
         * 判断报销申请单中是否已存在此数据
         *
         * 若存在  进行更新
         *
         * 如不存在 执行add方法
         *
         */
        String remind_id = remindPullDTO.getRemind_id();
        if(remind_id!=null&&!remind_id.equals("")){

            String sql = "select count(1) from remind where remind_id='"+remindPullDTO.getRemind_id()+"'";

            existName=getReportingToOne(sql);

            if(!existName.equals("0")) {
                return remindResource.updateRemindInfo(remindPullDTO);
            }
        }

        String Expen_Form_insert = "";
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");

            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time=df.format(new Date());
            Expen_Form_insert += "insert into remind(remind_id,remind_member_id,remind_content,remind_time,remind_type,remind_form_id" +
                    ",remind_order,remind_endtime,create_time,remind_starttime,remind_descritpion) VALUES('" + uuid + "','" +remindPullDTO.getRemind_member_id() + "','" + remindPullDTO.getRemind_content() + "','" +
                    remindPullDTO.getRemind_time() + "','" + remindPullDTO.getRemind_type() + "','" + remindPullDTO.getRemind_form_id()+"','" + 0 + "','" + remindPullDTO.getRemind_endtime()+"','" + time+"','" + remindPullDTO.getRemind_starttime()+"','" + remindPullDTO.getRemind_descritpion()+"')";
            //插入业务表
            resultls = addReportingTo(Expen_Form_insert);
            if (resultls != 0) {
//                resultDTO.setStr(remindPullDTO.getRemind_descritpion());
                resultDTO.setStr(uuid);
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.add_success);

            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.add_fail);
        }

        return resultDTO;
    }

    /**
     * 修改提醒
     * @param remindPullDTO
     * @return
     */
    @Override
    public ResultDTO updateRemindInfo(RemindPullDTO remindPullDTO) {
        String sql_isHas="";
        if (null != remindPullDTO.getRemind_id() && !"".equals(remindPullDTO.getRemind_id())) {
            sql_isHas = "select count(1) from remind WHERE remind_id='" + remindPullDTO.getRemind_id() + "'";
        }
        if (null != remindPullDTO.getRemind_form_id() && !"".equals(remindPullDTO.getRemind_form_id())) {
            sql_isHas = "select count(1) from remind WHERE remind_form_id='" + remindPullDTO.getRemind_form_id() + "'";
        }


        String existRemind = getReportingToOne(sql_isHas);


        int resultls = 0;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());

        //构建返回对象
        ResultDTO resultDTO = new ResultDTO();
        if (!existRemind.equals("0")) {
            String sql = "";
            String sql_w="";
            sql += "update remind set  create_time='" + time + "'";
            if (null != remindPullDTO.getRemind_member_id() && !"".equals(remindPullDTO.getRemind_member_id())) {
                sql += ",remind_member_id='" + remindPullDTO.getRemind_member_id() + "'";
            }

            if (null != remindPullDTO.getRemind_content() && !"".equals(remindPullDTO.getRemind_content())) {
                sql += ",remind_content ='" + remindPullDTO.getRemind_content() + "'";
            }
            if (null != remindPullDTO.getRemind_time() && !"".equals(remindPullDTO.getRemind_time())) {
                sql += ",remind_time ='" + remindPullDTO.getRemind_time() + "'";
            }
            if (null != remindPullDTO.getRemind_type() && !"".equals(remindPullDTO.getRemind_type())) {
                sql += ",remind_type ='" + remindPullDTO.getRemind_type() + "'";
            }

            if (null != remindPullDTO.getRemind_order() && !"".equals(remindPullDTO.getRemind_order())) {
                sql += ",remind_order ='" + remindPullDTO.getRemind_order() + "'";
            }

            if (null != remindPullDTO.getRemind_endtime() && !"".equals(remindPullDTO.getRemind_endtime())) {
                sql += ",remind_endtime='" + remindPullDTO.getRemind_endtime() + "'";
            }
            if (null != remindPullDTO.getCreate_time() && !"".equals(remindPullDTO.getCreate_time())) {
                sql += ",create_time='" + remindPullDTO.getCreate_time() + "'";
            }
            if (null != remindPullDTO.getRemind_starttime() && !"".equals(remindPullDTO.getRemind_starttime())) {
                sql += ",remind_starttime='" + remindPullDTO.getRemind_starttime() + "'";
            }
            if (null != remindPullDTO.getRemind_descritpion() && !"".equals(remindPullDTO.getRemind_descritpion())) {
                sql += ",remind_descritpion='" + remindPullDTO.getRemind_descritpion() + "'";
            }


            if (null != remindPullDTO.getRemind_id() && !"".equals(remindPullDTO.getRemind_id())) {
                sql_w += " AND remind_id='" + remindPullDTO.getRemind_id() + "'";
            }

            if (null != remindPullDTO.getRemind_form_id() && !"".equals(remindPullDTO.getRemind_form_id())) {
                sql += " AND remind_form_id ='" + remindPullDTO.getRemind_form_id() + "'";
            }
            sql += " where 1=1 " + sql_w  ;
            //更新会议室表
            resultls = addReportingTo(sql);
        }

        if (resultls != 0) {

            String sql = "select remind_id,remind_member_id,remind_content,remind_time,remind_type,remind_form_id,remind_order,remind_endtime,create_time,remind_starttime,remind_descritpion  from remind  where remind_id='" + remindPullDTO.getRemind_id() + "'";
            List<String[]> seresult = new ArrayList<String[]>();
            seresult = this.getReportingTo(sql);
//            RemindShowDTO remindShowDTO= (RemindShowDTO) remindResource.getRemindInfo("{remind_id:" + seresult.get(0)[1] + "}", "1", "1").getValues().get(0);
            if(seresult.get(0)[5].equals("9")){
//                NoticeShowDTO noticeShowDTO= (NoticeShowDTO) noticeResource.getNoticeInfo("{notice_id:" + remindPullDTO.getRemind_form_id() + "}", "1", "1").getValues().get(0);
                String sql2 ="select notice_types from notice where notice_id='" + seresult.get(0)[6] + "'";
                List<String[]>  seresult2 = this.getReportingTo(sql2);
                String  r[]=seresult2.get(0);
//                String types=noticeShowDTO.getNotice_types();
                NoticeMemberPullDTO noticeMemberPullDTO=new NoticeMemberPullDTO();
                noticeMemberPullDTO.setIsread("1");
                noticeMemberPullDTO.setMember_id(seresult.get(0)[2]);
                noticeMemberPullDTO.setNotice_id(seresult.get(0)[6]);
                noticeMemberPullDTO.setNotice_type(r[1]);
                noticeMemberResource.updateNoticeMemberInfo(noticeMemberPullDTO);
            }
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.update_success);

        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.update_fail);
        }

        return resultDTO;
    }

    /**
     * 删除提醒
     * @param remindPullDTO
     * @return
     */
    @Override
    public ResultDTO deleteRemindInfo(RemindPullDTO remindPullDTO) {
        //删除之前查看该表是否有记录
        String sql = "select count(1) from remind where remind_id='" + remindPullDTO.getRemind_id() + "'";
        String existName = getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();

        if (!existName.equals("0")) {
            String QUERY = "";
            QUERY += "delete from remind where remind_id='" + remindPullDTO.getRemind_id() + "'";

            result = this.addReportingTo(QUERY);
        }

        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);

        }
        return resultDTO;
    }
}
